package logic

import (
	"SeatSystem/internal/model"
	"SeatSystem/internal/repository"
	"SeatSystem/internal/response"
	"time"
)

// 寻找当前时间座位表内的可用座位
func ScreeningSeatTime(seats []response.SeatResponse, startTime, endTime time.Time) []response.SeatResponse {
	for k, seat := range seats {
		times, _ := repository.GetTimeList(seat.ID)
		flag := true
		for _, v := range times {
			if startTime.Unix() >= v.StartTime.Unix() && endTime.Unix() <= v.EndTime.Unix() {
				flag = false
				break
			}
		}
		if flag {
			seats[k].Selected = false //没选过
		}
	}
	return seats
}

func SortSeat(length int, seatList []response.SeatResponse) [][]response.SeatResponse {
	res := make([][]response.SeatResponse, len(seatList)/length+len(seatList)%length)
	if len(seatList) == 0 || length == 0 {
		res := make([][]response.SeatResponse, 1)
		res[0] = seatList
		return res
	}
	j := 0
	for i := 0; i < len(seatList); i += length {
		if i+length >= len(seatList) {
			res[j] = seatList[i:]
			break
		}
		res[j] = seatList[i : i+length]
		j++
	}
	return res
}

func ParseRspSeatList(seats []model.Seat) []response.SeatResponse {
	res := make([]response.SeatResponse, len(seats))
	for k, v := range seats {
		res[k] = parseRspSeat(v)
	}
	return res
}

func parseRspSeat(seat model.Seat) response.SeatResponse {
	return response.SeatResponse{
		ID:       seat.ID,
		CID:      seat.CID,
		SortID:   seat.SortID,
		Selected: true,
	}
}
